package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.vo.request.*;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceDetailResponse;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceItemResponse;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceLogisticsResponse;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceWaybillNumberResponse;
import com.jic.interaction.JDVOP.service.InvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Auther YGH
 * @DATE 2020/11/16/17:43
 */
@Service
@Slf4j
public class InvoiceServiceImpl implements InvoiceService {

    @Value("${route.JDVOP}")
    private String url;

    @Override
    public RestResult applyInvoice(InvoiceApplyRequest invoiceApplyRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 申请开票，接收参数。。{}", invoiceApplyRequest.toString());
        String u = url + "api/invoice/submit";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceApplyRequest));
        log.info("jdvop 申请开票，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    @Override
    public RestResult<String> selInvoiceByOrderId(InvoiceByOrderIdRequest invoiceByOrderIdRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票第三方申请单号，接收参数。。{}", invoiceByOrderIdRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceByOrderIdRequest));
        log.info("jdvop 查询发票第三方申请单号，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());

    }

    @Override
    public RestResult<List<InvoiceDetailResponse>> selInvoiceOutline(InvoiceOutlineRequest invoiceOutlineRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票概要，接收参数。。{}", invoiceOutlineRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceOutlineRequest));
        log.info("jdvop 查询发票概要，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        List<InvoiceDetailResponse> list = (List<InvoiceDetailResponse>) JSONObject.parseObject(m.get("result").toString());
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());

    }

    @Override
    public RestResult<InvoiceItemResponse> selInvoiceByIdorCodeList(InvoiceDetailedRequest invoiceDetailedRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票明细，接收参数。。{}", invoiceDetailedRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceDetailedRequest));
        log.info("jdvop 查询发票明细，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        InvoiceItemResponse invoiceItemResponse = JSONObject.parseObject(m.get("result").toString(), InvoiceItemResponse.class);
        return RestResult.success(invoiceItemResponse, m.get("resultCode").toString(), m.get("resultMessage").toString());

    }

    @Override
    public RestResult<List<T>> selElectronicsInvoiceByIdorType(InvoiceElectronicsDetailedRequest invoiceElectronicsDetailedRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询电子发票明细，接收参数。。{}", invoiceElectronicsDetailedRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceElectronicsDetailedRequest));
        log.info("jdvop 查询电子发票明细，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        List<T> list = JSONArray.parseArray(m.get("result").toString(), T.class);
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    @Override
    public RestResult<List<InvoiceWaybillNumberResponse>> selInvoiceByMarkIdList(InvoiceOutlineRequest invoiceOutlineRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票运单号，接收参数。。{}", invoiceOutlineRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceOutlineRequest));
        log.info("jdvop 查询发票运单号，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        List<InvoiceWaybillNumberResponse> list = JSONArray.parseArray(m.get("result").toString(), InvoiceWaybillNumberResponse.class);
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    @Override
    public RestResult<List<InvoiceLogisticsResponse>> selInvoiceByJdOrderIdList(InvoiceByOrderIdRequest invoiceByOrderIdRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票物流信息，接收参数。。{}", invoiceByOrderIdRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceByOrderIdRequest));
        log.info("jdvop 查询发票物流信息，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        List<InvoiceLogisticsResponse> list = JSONArray.parseArray(m.get("result").toString(), InvoiceLogisticsResponse.class);
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    @Override
    public RestResult<Boolean> CancelInvoice(InvoiceOutlineRequest invoiceOutlineRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 取消发票申请，接收参数。。{}", invoiceOutlineRequest.toString());
        String u = url + "api/invoice/queryThrApplyNo";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(invoiceOutlineRequest));
        log.info("jdvop 取消发票申请，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        Boolean b = (Boolean) m.get("result");
        return RestResult.success(b, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }


}
